package com.karson.service.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.karson.common.exception.AsmrException;
import com.karson.common.model.User;
import com.karson.common.request.UserSearchRequest;
import com.karson.common.request.UserStatusRequest;
import com.karson.common.response.UserResponse;
import com.karson.common.util.AsmrUtil;
import com.karson.common.util.DateUtil;
import com.karson.service.dao.UserMapper;
import com.karson.service.service.UserService;


import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author ZKS
* @description 针对表【ar_user(用户表)】的数据库操作Service实现
* @createDate 2025-07-01 21:51:24
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 根据手机号查询用户
     * @param phone 用户手机号
     * @return 用户信息
     */
    @Override
    public User getByPhone(String phone) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getPhone, phone);
        return userMapper.selectOne(userLambdaQueryWrapper);
    }

    @Override
    public User registerPhone(String phone, HttpServletRequest request) {
        User user = new User();
        user.setAccount(phone);
        user.setPhone(phone);
        user.setNickname("游客"+ AsmrUtil.randomCount(1111,9999));
        Date nowDate = DateUtil.nowDateTime();
        user.setCreatedAt(nowDate);
        user.setLastLoginTime(nowDate);
        user.setStatus(true);
        String ip = AsmrUtil.getClientIp(request);
        user.setAddIp(ip);
        boolean save = save(user);
        if (!save) {
            throw new AsmrException("注册失败");
        }
        return user;
    }

    @Override
    public IPage<UserResponse> pageList(IPage<UserResponse> userResponsePage, UserSearchRequest request) {
        return userMapper.pageList(userResponsePage,request);

    }

    @Override
    public UserResponse getInfo(Integer uid) {
        User user = userMapper.selectById(uid);
        return BeanUtil.copyProperties(user, UserResponse.class);
    }

    @Override
    public boolean delUser(Integer uid) {
        if (StpUtil.isLogin(uid)) {
            StpUtil.kickout(uid);
        }
        return removeById(uid);
    }



    @Override
    public boolean updateStatus(Integer uid, Boolean status) {
        LambdaUpdateWrapper<User> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.eq(User::getUid, uid);
        userUpdateWrapper.set(User::getStatus, status);
        return update(userUpdateWrapper);
    }
}




